package com.bit.musicserver.controller;


import com.bit.musicserver.Service.MusicService;
import com.bit.musicserver.Tools.Constat;
import com.bit.musicserver.Tools.ResponseBodyMessage;
import com.bit.musicserver.mapper.MusicMapper;
import com.bit.musicserver.model.Music;
import com.bit.musicserver.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.BindException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/music")
public class  MusicController {
    @Value("${music.local.path}")  //获取配置文件中的信息
    private String SAVE_PATH /*= "E:/localMusic/Music/"*/;
    @Autowired
    private MusicService musicService;
    @RequestMapping("/upload")
    public ResponseBodyMessage<Boolean> insertMusicController(@RequestParam String singer,
                                                    @RequestParam("filename") MultipartFile file,
                                                    HttpServletRequest request, HttpSession httpSession) {

      return musicService.insertMusic(singer,file,request,httpSession);
    }


    /*播放音乐的时候：music/get?path=xxx.mp3*/
    @RequestMapping("/get")
    public ResponseEntity<byte[]> getController(String path){  //ResponseEntity 这个和responsebody 用法类似
       return musicService.get(path);
    }

    /*删除单个音乐*/
    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> deleteMusicByIdController(@RequestParam("id") String id){
       return musicService.deleteMusicById(id);
    }


    /*批量删除音乐*/
    @RequestMapping("/deleteSel")
    public ResponseBodyMessage<Boolean> deleteMusicByUrlController( @RequestParam("id[]") List<Integer> id){
        return musicService.deleteMusicByUrl(id);
    }


    /*查询音乐*/
    @RequestMapping("/findmusic")
    public ResponseBodyMessage<List<Music>> findMusicController(@RequestParam(required = false) String musicName){
       return musicService.findMusic(musicName);
    }


}
